package com.controller;


import com.pojo.Dept;
import com.pojo.Page;
import com.pojo.Page;
import com.service.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Resource
    private DeptService deptService;
    @RequestMapping("/deptlist")
    @ResponseBody
    public Page queryDeptAll(int pageNo)
    {
        Page page = new Page(4,pageNo);
        List<Dept>list = deptService.queryDeptAll(page.getStartIndex(),page.getEndIndex());
        int row = deptService.queryDeptAllCount();
        page.setResult(list,row);
        return page;
    }

    @RequestMapping("/querydept")
    @ResponseBody
    public Page queryDeptByDeptName(String name,int pageNo)
    {
        Page page = new Page(4,pageNo);
        List<Dept>list = deptService.queryDeptByDeptName(name,page.getStartIndex(),page.getEndIndex());
        int row = deptService.queryDeptAllCount();
        page.setResult(list,row);
        return page;
    }


    @RequestMapping("/querydeptbyid")
    @ResponseBody
    public Dept queryDeptById(int id)
    {
       Dept dept = deptService.queryDeptByDeptId(id);
       return dept;
    }

    @RequestMapping("/adddept")
    @ResponseBody
    public String addDept(@RequestBody Dept dept)
    {
        int row  = deptService.addDept(dept);
        if(row>0)
        {
            return "{\"fig\":\"success\"}";
        }
        else
        {
            return "{\"fig\":\"error\"}";
        }
    }

    @RequestMapping("/deldept")
    @ResponseBody
    public String delDept(int id)
    {
        int row  = deptService.delDept(id);
        if(row>0)
        {
            return "{\"fig\":\"success\"}";
        }
        else
        {
            return "{\"fig\":\"error\"}";
        }
    }

    @RequestMapping("/updatedept")
    @ResponseBody
    public String updateDept (@RequestBody Dept dept)
    {
       int row = deptService.updateDept(dept);
       if(row>0)
       {
           return "{\"fig\":\"success\"}";
       }
       else
       {
           return "{\"fig\":\"error\"}";
       }
    }

}
